
#include <sqlite3.h>
#include <string>
#include <vector>
#include <iostream>
#include "../comm/log.hpp"

using namespace ns_log;
class SQLiteManager
{
public:
    SQLiteManager() = default; // 默认构造函数
    SQLiteManager(const std::string &dbName) : db(nullptr), dbName(dbName) {}

    ~SQLiteManager()
    {
        closeDatabase();
    }

    // 打开数据库
    bool openDatabase()
    {
        int result = sqlite3_open(dbName.c_str(), &db);
        if (result != SQLITE_OK)
        {
            errorMessage = "Cannot open database: " + std::string(sqlite3_errmsg(db));
            LOG("FATAL") << errorMessage << "\n";
            return false;
        }
        return true;
    }

    void closeDatabase()
    {
        if (db)
        {
            sqlite3_close(db);
            db = nullptr;
        }
    }

    bool executeQuery(const std::string &query)
    {
        char *errMsg = nullptr;
        int result = sqlite3_exec(db, query.c_str(), nullptr, nullptr, &errMsg);
        if (result != SQLITE_OK)
        {
            errorMessage = "SQL error: " + std::string(errMsg);
            sqlite3_free(errMsg);
            return false;
        }
        return true;
    }

    std::vector<std::vector<std::string>> executeSelect(const std::string &query)
    {
        LOG(DEBUG) << "执行语句: " << query << "\n";
        std::vector<std::vector<std::string>> result;
        char *errMsg = nullptr;
        char **results = nullptr;
        int rows, cols;

        int resultCode = sqlite3_get_table(db, query.c_str(), &results, &rows, &cols, &errMsg);
        if (resultCode != SQLITE_OK)
        {
            errorMessage = "SQL error: " + std::string(errMsg);
            sqlite3_free(errMsg);
            return result;
        }

        LOG(DEBUG) << "查询结果行数: " << rows << ", 列数: " << cols << "\n";

        if (results == nullptr)
        {
            errorMessage = "Results are null";
            return result;
        }

        // for (int i = 0; i < (rows + 1) * cols; ++i) {
        //     std::cout << "results[" << i << "] = " << (results[i] ? results[i] : "NULL") << std::endl;
        // }

        for (int i = 0; i < rows; ++i) // 从第一行数据开始
        {
            std::vector<std::string> row;
            for (int j = 0; j < cols; ++j)
            {
                row.push_back(results[(i + 1) * cols + j] ? results[(i + 1) * cols + j] : "");
            }
            result.push_back(row);
        }

        LOG(DEBUG) << "测试result查询结果: \n";
        for (const auto &row : result)
        {
            for (const auto &element : row)
            {
                std::cout << "元素: " + element + " ";
            }
            std::cout << "\n";
        }

        sqlite3_free_table(results);
        LOG(DEBUG) << "查询结果: " << result.size() << "行\n";
        return result;
    }

    std::string getErrorMessage() const
    {
        return errorMessage;
    }

    std::vector<std::vector<std::string>> getUserInfoByName(const std::string &name)
    {
        std::string query = "SELECT * FROM users WHERE name = '" + name + "';";
        return executeSelect(query);
    }

private:
    static int callback(void *data, int argc, char **argv, char **azColName)
    {
        // 函数回调: 功能待实现
        return 0;
    }

    sqlite3 *db;
    std::string dbName;
    std::string errorMessage;
};